<?php
global $valgt_aktivitet, $wpdb, $wp_locale;
$hide_right_sidebar = true;
$resultat = $wpdb->get_row("
    SELECT
        arrangement,
        arrangementtype,
        sted,
        DAY(fradato) AS fradag,
        MONTH(fradato) AS framnd,
        YEAR(fradato) AS fraår,
        fradato,
        HOUR(fratid) AS fratime,
        MINUTE(fratid) AS framin,
        fratid,
        DAY(tildato) AS tildag,
        MONTH(tildato) AS tilmnd,
        YEAR(tildato) AS tilår,
        tildato,
        HOUR(tiltid) AS tiltime,
        MINUTE(tiltid) AS tilmin,
        tiltid,
        heldag,
        offentlig,
        kommentar
    FROM arrangement
    WHERE arrangementid = $valgt_aktivitet");
$e = $resultat->fradag . '. ' . $wp_locale->get_month_abbrev($wp_locale->get_month($resultat->framnd)) . ' ' . $resultat->fraår;
if ($resultat->heldag != 1 && $resultat->fratid) {
    $e .= ' kl. ' . zeroise($resultat->fratime, 2) . ':' . zeroise($resultat->framin, 2);
}
if (($resultat->fradato != $resultat->tildato || $resultat->fratid != $resultat->tiltid) && ($resultat->tildato != null || $resultat->tiltid != null)) {
    $e .= ' - ';
    if ($resultat->tildato != null && $resultat->fradato != $resultat->tildato) {
        $e .= $resultat->tildag . '. ' . $wp_locale->get_month_abbrev($wp_locale->get_month($resultat->tilmnd)) . ' ' . $resultat->tilår;
        if ($resultat->heldag == 0 && $resultat-> tiltid != null) {
            $e .= ' kl. ';
        }
    }
    if ($resultat->heldag == 0 && $resultat-> tiltid != null) {
        $e .= zeroise($resultat->tiltime, 2) . ':' . zeroise($resultat->tilmin, 2);
    }
} ?>
<div id="rediger-aktivitet">
    <form method="post" id="red-aktivitet" action="<?php the_permalink() ?>">
        <p class="form-arr-navn">
            <label for="arr-navn">Navn: </label>
            <input class="text-input" name="arr-navn" type="text" id="arr-navn" value="<?php echo $resultat->arrangement ?>" style="float:right;"/>
        </p>
        <p>
            <label for="arr-type">Type: </label>
            <select name="arr-type" style="float:right;">
                <?php
                $a_typer = $wpdb->get_results("
                    SELECT feltdataid as fID, feltdata
                    FROM feltdata
                    WHERE felttype = 'arrangement'", ARRAY_A);
                foreach ($a_typer as $a_type) {
                    $sel = $a_type['fID'] == $resultat->arrangementtype; ?>
                <option <?php if ($sel) { ?>selected="true" <?php } ?>value="<?php echo $a_type['fID'] ?>"><?php echo $a_type['feltdata'] ?></option>
                <?php } ?>
            </select>
        </p>
        <p>
            <label for="arr-sted">Sted: </label>
            <input class="text-input" name="arr-sted" type="text" id="arr-sted" value="<?php echo $resultat->sted ?>" style="float:right;"/>
        </p>
        <p>
            <label for="arr-starter">Starter: </label>
            <a name="arr-starter" style="float:right;">
                <input class="text-input" name="arr-fradag" type="text" id="arr-fradag" value="<?php echo $resultat->fradag ?>" style="width:30px;"/>
                <input class="text-input" name="arr-framnd" type="text" id="arr-framnd" value="<?php echo $resultat->framnd ?>" style="width:30px;"/>
                <input class="text-input" name="arr-fraår" type="text" id="arr-fraår" value="<?php echo $resultat->fraår ?>" style="width:30px;"/> kl. 
                <input class="text-input" name="arr-fratime" type="text" id="arr-fratime" value="<?php echo zeroise(intval($resultat->fratime), 2) ?>" style="width:30px;"/>
                <input class="text-input" name="arr-framin" type="text" id="arr-framin" value="<?php echo zeroise(intval($resultat->framin), 2) ?>" style="width:30px;"/>
            </a>
        </p>
        <p>
        <p>
            <label for="arr-slutter">Slutter: </label>
            <a name="arr-slutter" style="float:right;">
                <input class="text-input" name="arr-tildag" type="text" id="arr-tildag" value="<?php echo $resultat->tildag ?>" style="width:30px;"/>
                <input class="text-input" name="arr-tilmnd" type="text" id="arr-tilmnd" value="<?php echo $resultat->tilmnd ?>" style="width:30px;"/>
                <input class="text-input" name="arr-tilår" type="text" id="arr-tilår" value="<?php echo $resultat->tilår ?>" style="width:30px;"/> kl. 
                <input class="text-input" name="arr-tiltime" type="text" id="arr-tiltime" value="<?php echo zeroise(intval($resultat->tiltime), 2) ?>" style="width:30px;"/>
                <input class="text-input" name="arr-tilmin" type="text" id="arr-tilmin" value="<?php echo zeroise(intval($resultat->tilmin), 2) ?>" style="width:30px;"/>
            </a>
        </p>
        <p>
            <label for="arr-heldag">Hele dagen: </label>
            <input name="arr-heldag" type="hidden" value="0"/>
            <input class="checkbox-input" name="arr-heldag" type="checkbox" id="arr-heldag" value="1" <?php if ($resultat->heldag == 1) { ?>checked="true" <?php } ?> style="float:right;"/>
        </p>
        <p>
            <label for="arr-offentlig">Offentlig: </label>
            <input name="arr-offentlig" type="hidden" value="0"/>
            <input class="checkbox-input" name="arr-offentlig" type="checkbox" id="arr-offentlig" value="1" <?php if ($resultat->offentlig == 1) { ?>checked="true" <?php } ?> style="float:right;"/>
        </p>
        <p>
            <label for="kommentar">Kommentar: </label>
            <textarea name="kommentar" id="arr-komm" rows="3" cols="50"><?php echo $resultat->kommentar ?></textarea>
        </p>
        <p class="form-submit">
            <input name="action" type="submit" class="submit button" value="Legg til" style="display:inline;"/>
            <input name="action" type="submit" class="button" value="Avbryt" style="display:inline;"/>
            <input name="id-value" type="hidden" value="<?php echo $valgt_aktivitet; ?>"
        </p>
    </form>
</div>